package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static com.sky.constant.MessageConstant.DISH_BE_RELATED_BY_SETMEAL;
import static com.sky.constant.MessageConstant.DISH_ON_SALE;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;



    @Transactional
    @Override
    public void addWithFlavor(DishDTO dishDto) {
        //1.往dish表中添加数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDto, dish, "flavors");
        dishMapper.add(dish);
        //2.往dish_flavor表中添加数据

        //拿到自动生成的dish_id
        Long id = dish.getId();
        //为每一种口味设置对应的dish_id
        List<DishFlavor> flavors = dishDto.getFlavors();
        if(flavors.size()==0){
            return;
        }
        flavors = flavors.stream().map(item -> {
            item.setDishId(id);
            return item;
        }).collect(Collectors.toList());
        //不使用for循环来一个一个插入是为了减缓数据库的压力
        //选择直接批量插入
        dishFlavorMapper.addBatch(flavors);
        /*for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
            dishFlavorMapper.add(flavor);
        }*/
    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.page(dishPageQueryDTO);
        long total = page.getTotal();
        List<DishVO> result = page.getResult();

        return new PageResult(total, result);
    }

    @Override
    public void deleteByids(Long[] ids) {
        //1.在售的菜品不能删除
        int count1 = dishMapper.countByStatus(ids);
        if (count1 > 0) {
            throw new DeletionNotAllowedException(DISH_ON_SALE);
        }
        //2.当前菜品关联套餐的话不删除
        int count2 = setmealDishMapper.countByDishId(ids);
        if (count2 > 0) {
            throw new DeletionNotAllowedException(DISH_BE_RELATED_BY_SETMEAL);
        }
        //3.删除菜品
        dishMapper.deleteByids(ids);
    }

    @Override
    public DishVO getByIdWithFlavor(Long id) {
        DishVO dishvo = dishMapper.getById(id);
        List<DishFlavor> flavors = dishFlavorMapper.getByDishId(id);
        dishvo.setFlavors(flavors);
        return dishvo;
    }

    @Transactional
    @Override
    public void update(DishDTO dishDto) {
        String key = "dish_" + dishDto.getCategoryId();
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDto, dish);
        dishMapper.update(dish);

        dishFlavorMapper.deleteByDishId(dishDto.getId());

        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map(item -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorMapper.addBatch(flavors);
    }

    @Override
    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        List<Dish> list = dishMapper.list(dish);
        return list;
    }

    /**
     * 条件查询菜品和口味
     *
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d, dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }
        return dishVOList;
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Dish dish=Dish.builder()
                .status(status)
                .id(id)
                .build();
        dishMapper.update(dish);
    }

}
